package presentation;

import helper.PlanarImageWrapper;

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.TitledBorder;

import org.apache.log4j.Logger;

import at.fhv.framework.filter.PassivePullFilter;

import com.sun.media.jai.widget.DisplayJAI;

import filters.pull.DilatePullFilter;
import filters.pull.ErodePullFilter;
import filters.pull.MedianPullFilter;
import filters.pull.ROIPullFilter;
import filters.pull.SinkPullFilter;
import filters.pull.SourcePullFilter;
import filters.pull.ThresholdPullFilter;
import filters.push.DilatePushFilter;
import filters.push.ErodePushFilter;
import filters.push.MedianPushFilter;
import filters.push.ROIPushFilter;
import filters.push.SinkPushFilter;
import filters.push.SourcePushFilter;
import filters.push.ThresholdPushFilter;

public class ImageFrame extends JFrame
{
	private static final long serialVersionUID = 4466727656477133308L;
	private Logger _logger = Logger.getLogger(ImageFrame.class);

	private JPanel _originalImagePanel;
	private JPanel _buttonPanel;
	private JPanel _modifiedImageAfterPushPanel;
	private JPanel _modifiedImageAfterPullPanel;
		
	private JButton _pullButton;
	private JButton _pushButton;

	private String _image;
	private SinkPushFilter _sink;

	public ImageFrame(String image) 
	{
		initGUI();
		_image = image;
		_sink = new SinkPushFilter(this);
	}

	private void initGUI() 
	{
		_logger.debug("initializing gui...");
		
		try 
		{
			getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT));
			setSize(1100, 500);
			setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

			_originalImagePanel = new JPanel();
			
			_buttonPanel = new JPanel();
			_buttonPanel.setLayout(new GridLayout(2, 1));
			
			_pullButton = new JButton("pull >");
			_pullButton.addActionListener(new ActionListener() 
											{
												@Override
												public void actionPerformed(ActionEvent evt) 
												{
													_logger.debug("pull processing...");
													
													PassivePullFilter<PlanarImageWrapper, PlanarImageWrapper> root =
															new SinkPullFilter(
																	new DilatePullFilter(
																			new ErodePullFilter(
																					new MedianPullFilter(
																							new ThresholdPullFilter(
																									new ROIPullFilter(
																											new SourcePullFilter(
																													_image)))))));
								
													setModifiedImageAfterPull(new DisplayJAI(root.read().getImage()));
												}
											});

			_pushButton = new JButton("push >");
			_pushButton.addActionListener(new ActionListener() 
											{
												@Override
												public void actionPerformed(ActionEvent evt) 
												{
													_logger.debug("push processing...");
													
													new SourcePushFilter(_image, 
														new ROIPushFilter(
															new ThresholdPushFilter(
																new MedianPushFilter(
																	new ErodePushFilter(
																		new DilatePushFilter(_sink))))));
												}
											});
						
			_buttonPanel.add(_pullButton);
			_buttonPanel.add(_pushButton);

			_modifiedImageAfterPushPanel = new JPanel();
			_modifiedImageAfterPullPanel = new JPanel();
			
			JPanel modifiedImagePanel = new JPanel();
			modifiedImagePanel.setLayout(new GridLayout(2, 1));
			modifiedImagePanel.add(_modifiedImageAfterPullPanel);
			modifiedImagePanel.add(_modifiedImageAfterPushPanel);
			
			getContentPane().add(new JScrollPane(_originalImagePanel));
			getContentPane().add(_buttonPanel);
			getContentPane().add(new JScrollPane(modifiedImagePanel));
		} 
		catch (Exception ex) 
		{
			_logger.debug(ex.getMessage(), ex);
		}
	}

	public void setOriginalImage(DisplayJAI image)
	{
		_originalImagePanel.setBorder(new TitledBorder("original image"));
		_originalImagePanel.removeAll();
		_originalImagePanel.add(image);
	}

	public void setModifiedImageAfterPush(DisplayJAI image) 
	{
		_modifiedImageAfterPushPanel.setBorder(new TitledBorder("after push processing"));
		_modifiedImageAfterPushPanel.removeAll();
		_modifiedImageAfterPushPanel.add(image);
		
		this.getRootPane().revalidate();
	}
	
	public void setModifiedImageAfterPull(DisplayJAI image)
	{
		_modifiedImageAfterPullPanel.setBorder(new TitledBorder("after pull processing"));
		_modifiedImageAfterPullPanel.removeAll();
		_modifiedImageAfterPullPanel.add(image);
		
		this.getRootPane().revalidate();
	}
}
